package com.mybatisLike;


import com.batis.bean.User;
import com.batis.source.io.MYResources;
import com.batis.source.session.MYSqlSessionFactory;
import com.batis.source.session.MYSqlSessionFactoryBuilder;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.Reader;

/**
 * @author isam2016
 * @create 2021-08-25 3:28 PM
 */
public class MybatisTest {
    private static SqlSessionFactory sqlSessionFactory;
    private static MYSqlSessionFactory mysqlSessionFactory;
    private static Reader reader; // 字符
    static {
        try {
            reader = MYResources.getResourceAsReader("mybatis-config.xml");
            // 原始
//            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            mysqlSessionFactory = new MYSqlSessionFactoryBuilder().build(reader);
            System.out.println(mysqlSessionFactory);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 查找
     */
    public static void findEmployeerById(int id) {
        SqlSession session = null;
        try {
            session = sqlSessionFactory.openSession();
            User employeer = (User) session.selectOne(
                    "com.batis.mapper.UserMapper.getUserById", 1);
            if (employeer == null)
                System.out.println("null");
            else
                System.out.println(employeer);
        } finally {
            session.close();
        }
    }

    public static void main(String[] args) {
        findEmployeerById(1);
    }
}
